package com.xyht.sca_s.student_manage_system.modules.rxRegistrationAdvance.controller;


import com.xyht.sca_s.student_manage_system.auth.util.JwtTokenUtil;
import com.xyht.sca_s.student_manage_system.common.annotation.RequestPass;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.desEncodeAndDecode.annotation.SecretBody;
import com.xyht.sca_s.student_manage_system.modules.rxRegistrationAdvance.entity.req.RxRegistrationAdvanceStudentInfoAddReq;
import com.xyht.sca_s.student_manage_system.modules.rxRegistrationAdvance.entity.req.RxRegistrationAdvanceStudentInfoBatchReq;
import com.xyht.sca_s.student_manage_system.modules.rxRegistrationAdvance.service.RxRegistrationAdvanceStudentInfoService;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.concurrent.Future;

/**
 * <p>
 * 招生预报名学生信息 前端控制器
 * </p>
 *
 * @author XYHT
 * @since 2024-05-12
 */
@RestController
@RequestMapping("/rxRegistrationAdvance/studentInfo")
@SecretBody
public class RxRegistrationAdvanceStudentInfoController {

    @Resource
    private RxRegistrationAdvanceStudentInfoService rxRegistrationAdvanceStudentInfoService;
    @Resource
    private JwtTokenUtil jwtTokenUtil;

    @ApiOperation("获取预报名数据")
    @GetMapping("/getAdvanceData")
    public ResponseResult getAdvanceData() {
        return rxRegistrationAdvanceStudentInfoService.getAdvanceData();
    }

    @ApiOperation("查询学生信息")
    @GetMapping("/getAdvanceStuInfo")
    @RequestPass
    public ResponseResult getAdvanceStuInfo(@RequestParam(value = "idCard") String idCard) {
        return rxRegistrationAdvanceStudentInfoService.getAdvanceStuInfo(idCard);
    }


    @ApiOperation("提交预报名信息")
    @PostMapping("/advancePayment")
    @RequestPass
    public ResponseResult advancePayment(@RequestBody RxRegistrationAdvanceStudentInfoAddReq addReq) {
        return rxRegistrationAdvanceStudentInfoService.advancePayment(addReq);
    }

    @ApiOperation("修改学生信息")
    @PostMapping("/updateStuInfo")
    @RequestPass
    public ResponseResult updateStuInfo(@RequestBody RxRegistrationAdvanceStudentInfoAddReq addReq) {
        return rxRegistrationAdvanceStudentInfoService.updateStuInfo(addReq);
    }

    @ApiOperation("录取通知书下载")
    @GetMapping("/admissionDownload")
    @RequestPass
    public ResponseResult admissionDownload(@RequestParam("stuId") String stuId, HttpServletResponse response) {
        ResponseResult responseResult;
        try{
            Future<ResponseResult> future = rxRegistrationAdvanceStudentInfoService.admissionDownload(stuId,response);
            while (true){
                if (future.isDone()){
                    break;
                }
            }
            responseResult = future.get();
        }catch (Exception e){
            e.printStackTrace();
            responseResult = CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return responseResult;
    }

    @ApiOperation("获取学生信息列表")
    @GetMapping("/getStuInfoList")
    public ResponseResult getStuInfoList(@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                         @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                         @RequestParam(value = "stuName", required = false) String stuName,
                                         @RequestParam(value = "planId",required = false) String planId,
                                         @RequestParam(value = "isFilingStu",required = false) Integer isFilingStu,
                                         @RequestParam(value = "isStay",required = false) Integer isStay,
                                         @RequestParam(value = "middleSchoolId", required = false) String middleSchoolId,//初中毕业学校
                                         @RequestParam(value = "majorId", required = false) String majorId,//专业
                                         @RequestParam(value = "tel", required = false) String tel,
                                         @RequestParam(value = "idCard", required = false) String idCard) {
        return rxRegistrationAdvanceStudentInfoService.getStuInfoList(pageNum, pageSize, stuName, planId,  isStay, tel, middleSchoolId, majorId, idCard,isFilingStu);
    }

    @ApiOperation("学生信息列表导出")
    @PostMapping("/stu/batch/export")
    public ResponseResult exportStuInfoList(@RequestBody RxRegistrationAdvanceStudentInfoBatchReq exportReq,
                                            HttpServletResponse response) {
        ResponseResult responseResult;
        try {
            Future<ResponseResult> future = rxRegistrationAdvanceStudentInfoService.exportStuInfoList(exportReq, response);
            while (true) {
                if (future.isDone()) {
                    break;
                }
            }
            responseResult = future.get();
        } catch (Exception e) {
            e.printStackTrace();
            responseResult = CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return responseResult;
    }

    @ApiOperation(value = "预报名老系统数据信息批量导入(第一阶段缴费金额300)")
    @PostMapping("/batch/add")
    @RequestPass
    public ResponseResult batchAddOldStudentPaymentData(@RequestBody Map<String,Object> map, HttpServletRequest request){
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        ResponseResult commonCodeEnum;
        try{
            Future<ResponseResult> future = rxRegistrationAdvanceStudentInfoService.batchAddOldStudentPaymentData(map,user_id);
            while (true){
                if (future.isDone()){
                    commonCodeEnum = future.get();
                    break;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return commonCodeEnum;
    }

    @ApiOperation("单独添加缴费项记录表")
    @PostMapping("/addCategoryRecord")
    @RequestPass
    public ResponseResult addCategoryRecord(){
        return rxRegistrationAdvanceStudentInfoService.addCategoryRecord();
    }

    @ApiOperation("删除老系统数据")
    @PostMapping("/deletedOldData")
    @RequestPass
    public ResponseResult deletedOldData(){
        return rxRegistrationAdvanceStudentInfoService.deletedOldData();
    }

    @ApiOperation("导入老系统数据(有学生信息 但未缴费)")
    @PostMapping("/batch/add/twoOld")
    @RequestPass
    public ResponseResult batchAddOldPaymentData(@RequestBody Map<String,Object> map, HttpServletRequest request){
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        ResponseResult commonCodeEnum;
        try{
            Future<ResponseResult> future = rxRegistrationAdvanceStudentInfoService.batchAddOldPaymentData(map,user_id);
            while (true){
                if (future.isDone()){
                    commonCodeEnum = future.get();
                    break;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return commonCodeEnum;
    }
}

